package com.fengpanlong.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fengpanlong.entity.Contact;
import com.fengpanlong.entity.ContactNote;
import com.fengpanlong.entity.EmployeeProject;
import com.fengpanlong.service.ContactService;
import com.fengpanlong.service.MessageService;
import com.fengpanlong.util.Form;
import com.fengpanlong.util.StringUtil;

public class ContactServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getMethod();
		String m = request.getParameter("_m");
		if("GET".equalsIgnoreCase(method)) {
			if("contactNote".equals(m)){
				showAllContactNote(request,response);
			} else if("newContact".equals(m)) {
				newContact(request,response);
			} else if("delContact".equals(m)) {
				delContact(request,response);
			} else if("editContact".equals(m)) {
				editContact(request,response);
			} else {
				showContact(request,response);
			}
		} else {
			if("saveContact".equals(m)) {
				saveContact(request,response);
			} else if("addContactNote".equals(m)){
				addContactNote(request,response);
			} else if("editContact".equals(m)) {
				editContact(request,response);
			}
		}
	}
	/**
	 * 编辑联系人
	 * @param request
	 * @param response
	 */
	private void editContact(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String method = request.getMethod();
		if("GET".equalsIgnoreCase(method)) {
			String contactId = request.getParameter("id");
			Contact contact = ContactService.findContactByContactId(contactId);
			request.setAttribute("contact", contact);
			goTojsp(request, response, "editContact.jsp");
		} else {
			String id = request.getParameter("id");
			String weibo_pre = request.getParameter("weibo_pre");
			String weibo_last = request.getParameter("weibo_last");
			String weibo = weibo_pre + weibo_last;
			Contact contact = ContactService.findContactByContactId(id);
			Form.saveParam(request, contact);
			contact.setWeibo(weibo);
			ContactService.updateContact(contact);
			String mesContent =" 修改了名字为" + "<a href = \"contact.jspx?_m=showContact&id=" + id + "\">" + contact.getName() + "</a>的联系人。";
			MessageService.sendMessage(request,mesContent);
			showContact(request, response);
		}
	}
	/**
	 * 删除联系人
	 * @param request
	 * @param response
	 */
	private void delContact(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		String contactId = request.getParameter("id");
		Contact contact = ContactService.findContactByContactId(contactId);
		ContactService.delContact(contactId);
		String content =" 删除了名字为" + contact.getName() + "</a>的联系人。";
		MessageService.sendMessage(request,content);
		response.sendRedirect("contact.jspx");
	}
	/**
	 * 添加记录
	 * @param request
	 * @param response
	 */
	private void addContactNote(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String contactId = request.getParameter("id");
		String contactNoteId = StringUtil.getId();
		String createtime = StringUtil.formateTime();
		ContactNote contactNote = new ContactNote();
		Form.saveParam(request, contactNote);
		
		contactNote.setId(contactNoteId);
		contactNote.setCreatetime(createtime);
		
		EmployeeProject ep = (EmployeeProject)request.getSession().getAttribute("ep");
		ContactService.saveContactNote(contactNote,contactId,ep.getEmployeeId());
		Contact contact = ContactService.findContactByContactId(contactId);
		request.setAttribute("contact", contact);
		List<ContactNote> contactNoteList = ContactService.findContactNotesByContactIdAndEmployeeId(contactId,ep.getEmployeeId());
		request.setAttribute("contactNoteList", contactNoteList);
		List<Contact> contatcList = ContactService.findContactsByProjectId(ep.getProjectId());
		request.setAttribute("contactList", contatcList);
		String mesContent =" 发表了对名字为" + "<a href = \"contact.jspx?_m=showContact&id=" + contact.getId() + "\">" + contact.getName() + "</a>的联系人的评论。";
		MessageService.sendMessage(request,mesContent);
		goTojsp(request, response, "contactNote.jsp");
	}
	/**
	 * 保存联系人
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void saveContact(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		Contact contact = new Contact();
		Form.saveParam(request, contact);
		
		String id = StringUtil.getId();
		String weibo_pre = request.getParameter("weibo_pre");
		String weibo_last = request.getParameter("weibo_last");
		String weibo = weibo_pre + weibo_last;
		String createtime = StringUtil.formateTime();
		contact.setId(id);
		contact.setWeibo(weibo);
		contact.setCreatetime(createtime);
		EmployeeProject ep = (EmployeeProject)request.getSession().getAttribute("ep");
		ContactService.saveContact(contact,ep.getProjectId());
		String msgContent ="添加了名字为" + "<a href = \"contact.jspx?_m=showContact&id=" + id + "\">" + contact.getName() + "</a>的联系人。";
		MessageService.sendMessage(request,msgContent);
		showContact(request, response);
	}
	/**
	 * 到创建contact页面
	 * @param request
	 * @param response
	 */
	private void newContact(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		goTojsp(request, response, "newContact.jsp");
		
	}
	/**
	 * 显示所有记录
	 * @param request
	 * @param response
	 */
	private void showAllContactNote(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String contactId = request.getParameter("id");
		EmployeeProject ep = (EmployeeProject)request.getSession().getAttribute("ep");
		Contact contact = ContactService.findContactByContactId(contactId);
		request.setAttribute("contact", contact);
		List<ContactNote> contactNoteList = ContactService.findContactNotesByContactIdAndEmployeeId(contactId,ep.getEmployeeId());
		request.setAttribute("contactNoteList", contactNoteList);
		List<Contact> contatcList = ContactService.findContactsByProjectId(ep.getProjectId());
		request.setAttribute("contactList", contatcList);
		goTojsp(request, response, "contactNote.jsp");
	}
	/**
	 * 显示联系人资料
	 * @param request
	 * @param response
	 */
	private void showContact(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String contactId = request.getParameter("id");
		EmployeeProject ep = (EmployeeProject)request.getSession().getAttribute("ep");
		List<Contact> contatcList = ContactService.findContactsByProjectId(ep.getProjectId());
		request.setAttribute("contactList", contatcList);
		if(contactId == null) {
			if(contatcList.size() != 0) {
				contactId= contatcList.get(0).getId();
			}
		}
		Contact contact = ContactService.findContactByContactId(contactId);
		request.setAttribute("contact", contact);
		List<ContactNote> contactNoteList = ContactService.findContactNotesByContactIdAndEmployeeId(contactId,ep.getEmployeeId());
		request.setAttribute("contactNoteList", contactNoteList);
		goTojsp(request, response,"contact.jsp");
	}
	/**
	 * 去对应jsp页面
	 * @param request
	 * @param response
	 * @param jspName
	 * @throws ServletException
	 * @throws IOException
	 */
	private void goTojsp(HttpServletRequest request,
			HttpServletResponse response, String jspName) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/views/" + jspName).forward(request, response);
	}
}
